<?php

namespace app\common\logic;

use app\common\model\Order;
use think\Db;
use think\Model;

/**
 * 订单逻辑类
 */
class OrderLogic extends Model
{
    protected $order; //订单模型

    /**
     * 检查活动订单是否可以下单
     * @param $order_prom_type 订单活动类型:0=默认,1=砍价,2=拼团,3=秒杀,4=优惠促销
     * @param $goods_id
     * @param $order_prom_id
     * @param int $num
     */
    public function PromIsCanAddOrder($order_prom_type, $goods_id, $order_prom_id, $num = 1)
    {
        $goods = \app\common\model\Goods::get($goods_id);

        $goodsPromFactory = new \app\common\logic\GoodsPromFactory();
        if (!empty($order_prom_id) && $goodsPromFactory->checkPromType($order_prom_type)) {
            $goodsPromLogic = $goodsPromFactory->makeModule($goods);//这里会自动更新商品活动状态，所以商品需要重新查询
            if ($order_prom_type == 1) {
                //砍价
//            $cutdownLogic = new \app\common\logic\CutdownLogic($goods);
//            $result = $cutdownLogic->IsAble();
                $result = $goodsPromLogic->IsAble();
                if (!$result) {

                    return ['code' => 0, 'msg' => '该活动已失效', 'data' => ''];

                } else {
                    return ['code' => 1, 'msg' => '', 'data' => ''];
                }


            } elseif ($order_prom_type == 2) {
                //拼团

                $teamM = new \app\common\model\TeamActivity();
                $team = $teamM->get($order_prom_id);
                $teamOrderLogic = new \app\common\logic\TeamOrderLogic();
                $teamOrderLogic->setTeam($team);
                $teamOrderLogic->setGoods($goods);
                $teamOrderLogic->setGoodsBuyNum($num);
                $can = $teamOrderLogic->teamIsCanAddOrder();

                if ($can['code'] == 0) {

                    return ['code' => 0, 'msg' => $can['msg'], 'data' => ''];

                } else {
                    return ['code' => 1, 'msg' => '', 'data' => ''];
                }
            } elseif ($order_prom_type == 3) {
                //秒杀
                $result = $goodsPromLogic->IsAble();
                if (!$result) {

                    return ['code' => 0, 'msg' => '该活动已失效', 'data' => ''];

                } else {
                    return ['code' => 1, 'msg' => '', 'data' => ''];
                }


            } elseif ($order_prom_type == 4) {
                //优惠促销
                $result = $goodsPromLogic->IsAble();
                if (!$result) {

                    return ['code' => 0, 'msg' => '该活动已失效', 'data' => ''];

                } else {
                    return ['code' => 1, 'msg' => '', 'data' => ''];
                }
            }
        } else {
            return ['code' => 0, 'msg' => '参数不正确', 'data' => ''];
        }


    }

    public function setOrder($order)
    {
        $this->order = $order;
    }

    /**
     *取消订单
     */
    public function cancelOrder()
    {
        if (empty($this->order)) {
            return ['code' => 0, 'msg' => '订单不存在', 'data' => ''];
        }

        if ($this->order['pay_status'] > 0 || $this->order['order_status'] > 0) {
            return ['code' => 0, 'msg' => '支付状态或订单状态不允许', 'data' => ''];
        }

        //是否有积分支付情况
//        if ($this->order['integral'] > 0) {
//            $memo = "订单取消，退回" . $this->order['integral'] . "积分";
//            change_user_score_log($this->order['user_id'], $this->order['integral'], $memo, $this->order['order_sn'], $this->order['id']);
//
//
//        }

        $this->order->order_status = 3;
        $this->order->user_note = '用户取消订单';
        $result = $this->order->save();

        if ($result !== false) {
            return ['code' => 1, 'msg' => '订单取消成功', 'data' => ''];
        } else {
            return ['code' => 0, 'msg' => '订单取消失败', 'data' => ''];
        }

    }

    /**
     * 订单确认收货
     * @param $id   订单id
     */
    function confirm_order($id, $user_id = 0)
    {
        $where['order_id'] = $id;
        if ($user_id) {
            $where['user_id'] = $user_id;
        }
        $order = Db::name('order')->where($where)->find();

        if ($order['order_status'] != 1 || empty($order['pay_time']) || $order['pay_status'] != 1)
            return array('code' => -1, 'msg' => '该订单不能收货确认');

        $data['order_status'] = 2; // 已收货
        $data['pay_status'] = 1; // 已付款
        $data['confirm_time'] = time(); //  收货确认时间

        $row = Db::name('order')->where(array('order_id' => $id))->update($data);

        if (!$row)
            return array('code' => -3, 'msg' => '操作失败');
        if($order['order_prom_type'] != 5){  //不是虚拟订单送东西
            order_give($order);
        }

        return array('code' => 1, 'msg' => '操作成功');
    }
}